<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="关联会员id" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入关联会员id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行驶证车辆类型" prop="carType">
        <el-input
          v-model="queryParams.carType"
          placeholder="请输入行驶证车辆类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所有人" prop="carOwner">
        <el-input
          v-model="queryParams.carOwner"
          placeholder="请输入所有人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌型号" prop="brandModel">
        <el-input
          v-model="queryParams.brandModel"
          placeholder="请输入品牌型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆识别代号" prop="vinNum">
        <el-input
          v-model="queryParams.vinNum"
          placeholder="请输入车辆识别代号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发动机/电动机号码" prop="engineNum">
        <el-input
          v-model="queryParams.engineNum"
          placeholder="请输入发动机/电动机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册日期" prop="regDate">
        <el-input
          v-model="queryParams.regDate"
          placeholder="请输入注册日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最新年审周期" prop="annualDate">
        <el-input
          v-model="queryParams.annualDate"
          placeholder="请输入最新年审周期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下个年审周期" prop="nextAnnualDate">
        <el-input
          v-model="queryParams.nextAnnualDate"
          placeholder="请输入下个年审周期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发证日期" prop="issueDate">
        <el-input
          v-model="queryParams.issueDate"
          placeholder="请输入发证日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用性质  1非营运  2 营运" prop="useType">
        <el-select v-model="queryParams.useType" placeholder="请选择使用性质  1非营运  2 营运" clearable size="small">
          <el-option
            v-for="dict in useTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="燃油类型  1汽油 2柴油 3新能源" prop="fuelType">
        <el-select v-model="queryParams.fuelType" placeholder="请选择燃油类型  1汽油 2柴油 3新能源" clearable size="small">
          <el-option
            v-for="dict in fuelTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有人伤事故  0 无  1有" prop="hasAccident">
        <el-input
          v-model="queryParams.hasAccident"
          placeholder="请输入是否有人伤事故  0 无  1有"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="座位数" prop="seat">
        <el-input
          v-model="queryParams.seat"
          placeholder="请输入座位数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否默认选中   0否  1是" prop="isDefault">
        <el-input
          v-model="queryParams.isDefault"
          placeholder="请输入是否默认选中   0否  1是"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统设定车辆类型" prop="sysCarType">
        <el-select v-model="queryParams.sysCarType" placeholder="请选择系统设定车辆类型" clearable size="small">
          <el-option
            v-for="dict in sysCarTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="行驶证车辆性质" prop="useTypeDesp">
        <el-input
          v-model="queryParams.useTypeDesp"
          placeholder="请输入行驶证车辆性质"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="gmtTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.gmtTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:store:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['car:store:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['car:store:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['car:store:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="关联会员id" align="center" prop="memberId" />
      <el-table-column label="车牌号" align="center" prop="carNum" />
      <el-table-column label="行驶证车辆类型" align="center" prop="carType" />
      <el-table-column label="所有人" align="center" prop="carOwner" />
      <el-table-column label="品牌型号" align="center" prop="brandModel" />
      <el-table-column label="车辆识别代号" align="center" prop="vinNum" />
      <el-table-column label="发动机/电动机号码" align="center" prop="engineNum" />
      <el-table-column label="注册日期" align="center" prop="regDate" />
      <el-table-column label="最新年审周期" align="center" prop="annualDate" />
      <el-table-column label="下个年审周期" align="center" prop="nextAnnualDate" />
      <el-table-column label="发证日期" align="center" prop="issueDate" />
      <el-table-column label="使用性质  1非营运  2 营运" align="center" prop="useType" :formatter="useTypeFormat" />
      <el-table-column label="燃油类型  1汽油 2柴油 3新能源" align="center" prop="fuelType" :formatter="fuelTypeFormat" />
      <el-table-column label="是否有人伤事故  0 无  1有" align="center" prop="hasAccident" />
      <el-table-column label="座位数" align="center" prop="seat" />
      <el-table-column label="是否默认选中   0否  1是" align="center" prop="isDefault" />
      <el-table-column label="系统设定车辆类型" align="center" prop="sysCarType" :formatter="sysCarTypeFormat" />
      <el-table-column label="行驶证车辆性质" align="center" prop="useTypeDesp" />
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['car:store:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['car:store:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户车辆(Api接口)传递过来的对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联会员id" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入关联会员id" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNum">
          <el-input v-model="form.carNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="行驶证车辆类型" prop="carType">
          <el-input v-model="form.carType" placeholder="请输入行驶证车辆类型" />
        </el-form-item>
        <el-form-item label="所有人" prop="carOwner">
          <el-input v-model="form.carOwner" placeholder="请输入所有人" />
        </el-form-item>
        <el-form-item label="品牌型号" prop="brandModel">
          <el-input v-model="form.brandModel" placeholder="请输入品牌型号" />
        </el-form-item>
        <el-form-item label="车辆识别代号" prop="vinNum">
          <el-input v-model="form.vinNum" placeholder="请输入车辆识别代号" />
        </el-form-item>
        <el-form-item label="发动机/电动机号码" prop="engineNum">
          <el-input v-model="form.engineNum" placeholder="请输入发动机/电动机号码" />
        </el-form-item>
        <el-form-item label="注册日期" prop="regDate">
          <el-input v-model="form.regDate" placeholder="请输入注册日期" />
        </el-form-item>
        <el-form-item label="最新年审周期" prop="annualDate">
          <el-input v-model="form.annualDate" placeholder="请输入最新年审周期" />
        </el-form-item>
        <el-form-item label="下个年审周期" prop="nextAnnualDate">
          <el-input v-model="form.nextAnnualDate" placeholder="请输入下个年审周期" />
        </el-form-item>
        <el-form-item label="发证日期" prop="issueDate">
          <el-input v-model="form.issueDate" placeholder="请输入发证日期" />
        </el-form-item>
        <el-form-item label="使用性质  1非营运  2 营运">
          <el-select v-model="form.useType" placeholder="请选择使用性质  1非营运  2 营运">
            <el-option
              v-for="dict in useTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="燃油类型  1汽油 2柴油 3新能源">
          <el-select v-model="form.fuelType" placeholder="请选择燃油类型  1汽油 2柴油 3新能源">
            <el-option
              v-for="dict in fuelTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否有人伤事故  0 无  1有" prop="hasAccident">
          <el-input v-model="form.hasAccident" placeholder="请输入是否有人伤事故  0 无  1有" />
        </el-form-item>
        <el-form-item label="座位数" prop="seat">
          <el-input v-model="form.seat" placeholder="请输入座位数" />
        </el-form-item>
        <el-form-item label="是否默认选中   0否  1是" prop="isDefault">
          <el-input v-model="form.isDefault" placeholder="请输入是否默认选中   0否  1是" />
        </el-form-item>
        <el-form-item label="系统设定车辆类型">
          <el-select v-model="form.sysCarType" placeholder="请选择系统设定车辆类型">
            <el-option
              v-for="dict in sysCarTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行驶证车辆性质" prop="useTypeDesp">
          <el-input v-model="form.useTypeDesp" placeholder="请输入行驶证车辆性质" />
        </el-form-item>
        <el-form-item label="创建时间" prop="gmtTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.gmtTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStore, getStore, delStore, addStore, updateStore, exportStore } from "@/api/member/carStore";

export default {
  name: "Store",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户车辆(Api接口)传递过来的表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 使用性质  1非营运  2 营运字典
      useTypeOptions: [],
      // 燃油类型  1汽油 2柴油 3新能源字典
      fuelTypeOptions: [],
      // 系统设定车辆类型字典
      sysCarTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: undefined,
        carNum: undefined,
        carType: undefined,
        carOwner: undefined,
        brandModel: undefined,
        vinNum: undefined,
        engineNum: undefined,
        regDate: undefined,
        annualDate: undefined,
        nextAnnualDate: undefined,
        issueDate: undefined,
        useType: undefined,
        fuelType: undefined,
        hasAccident: undefined,
        seat: undefined,
        isDefault: undefined,
        sysCarType: undefined,
        useTypeDesp: undefined,
        gmtTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("car_use_type").then(response => {
      this.useTypeOptions = response.data;
    });
    this.getDicts("car_fuel_type").then(response => {
      this.fuelTypeOptions = response.data;
    });
    this.getDicts("car_type").then(response => {
      this.sysCarTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询用户车辆(Api接口)传递过来的列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then(response => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 使用性质  1非营运  2 营运字典翻译
    useTypeFormat(row, column) {
      return this.selectDictLabel(this.useTypeOptions, row.useType);
    },
    // 燃油类型  1汽油 2柴油 3新能源字典翻译
    fuelTypeFormat(row, column) {
      return this.selectDictLabel(this.fuelTypeOptions, row.fuelType);
    },
    // 系统设定车辆类型字典翻译
    sysCarTypeFormat(row, column) {
      return this.selectDictLabel(this.sysCarTypeOptions, row.sysCarType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        memberId: undefined,
        carNum: undefined,
        carType: undefined,
        carOwner: undefined,
        brandModel: undefined,
        vinNum: undefined,
        engineNum: undefined,
        regDate: undefined,
        annualDate: undefined,
        nextAnnualDate: undefined,
        issueDate: undefined,
        useType: undefined,
        fuelType: undefined,
        hasAccident: undefined,
        seat: undefined,
        isDefault: undefined,
        sysCarType: undefined,
        useTypeDesp: undefined,
        gmtTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户车辆(Api接口)传递过来的";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStore(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户车辆(Api接口)传递过来的";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateStore(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addStore(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户车辆(Api接口)传递过来的编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStore(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户车辆(Api接口)传递过来的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStore(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
